<template>
  <div class="layout-warpper">
    <el-tabs v-model="topTabActive" @tab-click="tabClickFn">
      <el-tab-pane name="权重平均分配">
        <span slot="label">权重平均分配</span>
        <!--
			"distributeRuleId": "1",
			"witnessLinkName": "资审",
			"allocateType": "CREATE",
			"weight": 3,
			"averageDistribution": null -->
        <el-table key="table-wight" :data="wightTableData" v-loading="loading" isdrag highlight-current-row>
          <el-table-column type="index" :index="indexFn" label="序号" width="60px" align="center"></el-table-column>
          <el-table-column prop="witnessLinkName" label="见证环节" />
          <el-table-column prop="allocateType" label="分配时机">
            <template slot-scope="scope">
              {{ findLabelFn(scope.row.allocateType, scope.row.witnessLinkName) }}
            </template>
          </el-table-column>
          <el-table-column prop="weight" label="权重" />
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="text" size="default" @click="updateDate(scope)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane name="任务量平均分配">
        <span slot="label">任务量平均分配</span>
        <!-- {{ numberTableData }} -->
        <!-- {
			"distributeRuleId": "1",
			"witnessLinkName": "招标",
			"allocateType": "CREATE",
			"weight": null,
			"averageDistribution": "未结束见证的招标+投标+定标任务总量"
		} -->
        <el-table key="tablenumber" :data="numberTableData" v-loading="loading" isdrag highlight-current-row>
          <el-table-column type="index" :index="indexFn" label="序号" width="60px" align="center"></el-table-column>
          <el-table-column prop="witnessLinkName" label="见证环节" />
          <el-table-column prop="allocateType" label="分配时机">
            <template slot-scope="scope">
              {{ findLabelFn(scope.row.allocateType, scope.row.witnessLinkName) }}
            </template>
          </el-table-column>
          <el-table-column prop="averageDistribution" label="平均分配依据" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页 -->
    <!-- <div class="layout-footer ui-pt10">
      <pagination
        v-if="pageObj.total >= pageObj.pageSizes[0]"
        :total="pageObj.total"
        :page.sync="pageObj.currentPage"
        :limit.sync="pageObj.pageSize"
        :pageSizes="pageObj.pageSizes"
        @pagination="subtaskListFn"
      />
    </div> -->
    <!-- 编辑弹框
    自动分配规则说明（详细规则见企微在线文档）：
权重平均分配：
1.默认显示图中所示平均分配规则，用户可点击编辑，对权重和分配时机进行编辑
2.针对开标、清标、评标，编辑弹窗中可编辑相应的分配时机和任务权重，见证环节不可编辑，任务权重下拉列表均为1~10的整数
开标任务的分配时机下拉列表：任务创建时分配、开标当日分配
评标任务的分配时机下拉列表：任务创建时分配、评标当日分配
清标任务的分配时机下拉列表：任务创建时分配、清标当日分配
3.针对资审，编辑弹窗中可编辑任务权重，权重下拉列表为1~10的整数
任务量平均分配：
1.默认显示图中所示平均分配规则，不可编辑
-->
    <el-dialog
      title="编辑见证事项"
      :visible.sync="editVisible"
      width="500px"
      class="witness-dialog"
      draggable
      :show-close="true"
      :close-on-click-modal="false"
      @close="closeDialogFn"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rulesData"
        label-width="107px"
        :inline="false"
        size="normal"
        on-submit="return false;"
      >
        <el-form-item label="见证环节" prop="witnessLinkName">
          <el-input
            v-model="formData.witnessLinkName"
            maxlength="100"
            :disabled="true"
            placeholder="请输入内容"
            style="width: 290px"
          />
        </el-form-item>
        <el-form-item label="分配时机" prop="allocateType">
          <el-select
            v-model="formData.allocateType"
            :disabled="formData.witnessLinkName === '资审'"
            style="width: 290px"
          >
            <el-option
              v-for="item in TASKOBJ.witnessAllocateType"
              :key="item.value"
              :label="findLabelFn(item.value, formData.witnessLinkName)"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权重" prop="weight">
          <el-select v-model="formData.weight" style="width: 290px">
            <el-option v-for="item in 10" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="text-align: right; margin-top: 10px">
          <el-button type="primary" :loading="loadingBtn" @click="editSubmitFn('formData')">确定</el-button>
          <el-button @click="cancelSubmitFnBJ('formData')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  // import ExpertList from "./expertList.vue";
  // import expertSignIn from "./expertSignIn.vue";
  import {
    moduleInit,
    taskConfigUpdateWight,
    witnessTaskConfigListWight,
    witnessTaskConfigListNumber
  } from "../../../api/api.js";
  export default {
    components: {},
    data() {
      return {
        tableData: [],
        wightTableData: [],
        numberTableData: [],
        topTabActive: "权重平均分配",
        xugai: "",
        TASKOBJ: {},
        loadingBtn: false,
        loading: false,
        deleteLiveVisible: false,
        editVisible: false,
        formData: {},
        formSearch: {},
        rulesData: {
          allocateType: [{ required: true, message: "请选择分配时机", trigger: "blur" }],
          weight: [
            {
              required: true,
              message: "请选择权重",
              trigger: ["blur", "change"]
            }
          ],
          witnessLinkName: [{ required: true, message: "请选择见证环节", trigger: "change" }]
        },
        pageObj: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [10, 20, 30]
        }
      };
    },
    async created() {
      await this.moduleInitFn();
      await this.witnessTaskConfigListWightFn();
    },
    methods: {
      // 页面初始化
      async moduleInitFn() {
        const p = { moduleName: "TASK" };
        try {
          const res = await moduleInit(p);
          if (res.success) {
            this.TASKOBJ = res.data;
          } else {
            this.$message({
              type: "error",
              message: res.message || "获取数据失败，请稍后再试",
              single: true
            });
          }
        } catch (err) {
          this.$message({
            type: "error",
            message: "获取数据失败，请稍后再试",
            single: true
          });
        }
      },
      tabClickFn() {
        this.topTabActive;
        if (this.topTabActive === "权重平均分配") {
          this.witnessTaskConfigListWightFn();
        } else {
          this.witnessTaskConfigListNumberFn();
        }
      },
      // 编辑
      updateDate({ row, $index }) {
        this.weightIndex = $index;
        this.formData = JSON.parse(JSON.stringify(row));
        this.editVisible = true;
      },
      closeDialogFn() {
        this.editVisible = false;
      },
      indexFn(index) {
        return index + 1 + (this.pageObj.currentPage - 1) * this.pageObj.pageSize;
      },
      // 确认编辑
      editSubmitFn(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loadingBtn = true;
            this.taskConfigUpdateWightFn();
          } else {
            return false;
          }
        });
      },
      cancelSubmitFnBJ() {
        this.editVisible = false;
      },
      findLabelFn(val, name) {
        const obj = this.TASKOBJ.witnessAllocateType.find((item) => item.value === val);
        const str = val === "CREATE" ? "" : name;
        return str + obj.label;
      },
      // 权重平均分配
      async witnessTaskConfigListWightFn() {
        this.loading = true;
        try {
          const p = {};
          const res = await witnessTaskConfigListWight(p);
          if (res.success) {
            this.wightTableData = res.data;
          } else {
            this.$message({
              type: "error",
              message: "获取数据失败，请稍后再试",
              single: true
            });
          }
        } catch (err) {
          console.error(err);
          this.$message({
            type: "error",
            message: "获取数据失败，请稍后再试",
            single: true
          });
        }
        this.loading = false;
      },
      // 任务量平均分配
      async witnessTaskConfigListNumberFn() {
        this.loading = true;
        try {
          const p = {};
          const res = await witnessTaskConfigListNumber(p);
          if (res.success) {
            this.numberTableData = res.data;
          } else {
            this.$message({
              type: "error",
              message: "获取数据失败，请稍后再试",
              single: true
            });
          }
        } catch (err) {
          console.error(err);
          this.$message({
            type: "error",
            message: "获取数据失败，请稍后再试",
            single: true
          });
        }
        this.loading = false;
      },
      async taskConfigUpdateWightFn() {
        try {
          const p = {
            allocateType: this.formData.allocateType,
            weight: this.formData.weight,
            witnessLinkName: this.formData.witnessLinkName
          };
          const res = await taskConfigUpdateWight(p);
          if (res.success) {
            this.editVisible = false;
            const obj = Object.assign({}, this.wightTableData[this.weightIndex], p);
            this.$set(this.wightTableData, this.weightIndex, obj);
            this.$message({
              type: "success",
              message: "修改成功！",
              single: true
            });
          } else {
            this.$message({
              type: "error",
              message: "获取数据失败，请稍后再试",
              single: true
            });
          }
        } catch (err) {
          console.error(err);
          this.$message({
            type: "error",
            message: "获取数据失败，请稍后再试",
            single: true
          });
        }
        this.loadingBtn = false;
      },
      async handleSearchFn(e) {
        this.pageObj.total = 0;
        this.pageObj.currentPage = 1;
      }
    }
  };
</script>
<style lang="scss" scoped></style>
